package com.jsle.financial.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsle.financial.vo.Page;

/**
 * @Title: AJAXUtil.java
 * @Description: <br>
 *               <br>
 * @author wisdying
 * date 2012-10-31
 */
public class AJAXUtil {

	public static void writerJson(HttpServletResponse response,String json){
		try {
			response.getWriter().print(json);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
    public static Map<String, Object> success() {
	Map<String, Object> resultMap = new HashMap<String, Object>();
	resultMap.put("success", true);
	return resultMap;
    }

	public static void setCharacterEncoding(HttpServletRequest request,HttpServletResponse response,String encodingStr){
		try {
			request.setCharacterEncoding(encodingStr);
			response.setCharacterEncoding(encodingStr);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void setCharacterEncoding(HttpServletRequest request,String encodingStr){
		try {
			request.setCharacterEncoding(encodingStr);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void setCharacterEncoding(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf8");
			response.setCharacterEncoding("utf8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void setCharacterEncoding(HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void setCharacterEncoding(HttpServletResponse response){
		try {
			response.setCharacterEncoding("gbk");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    public static Map pageResult(Page page) {
	Map<String, Object> resultMap = new HashMap<String, Object>();
	resultMap.put("success", true);
	resultMap.put("pageCount", page.getPageCount());
	resultMap.put("totalCount", page.getTotalCount());
	resultMap.put("data", page.getData());
	return resultMap;
    }
}
